package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.Vo.ClassificationVo;
import cn.leyaber.hrm.clients.UserFeignClient;
import cn.leyaber.hrm.domain.Classification;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.query.ClassificationQuery;
import cn.leyaber.hrm.service.IClassificationService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/classification")
public class ClassificationController {
    @Autowired
    private IClassificationService classificationService;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
    * 保存和修改公用的
    * @param classification  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Classification classification){
        try {
            if(classification.getId()!=null){
                classificationService.updateById(classification);
            }else{
                classificationService.insert(classification);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            classificationService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Classification get(@PathVariable("id")Long id)
    {
        return classificationService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<ClassificationVo> selectClassificationVo(){

        return classificationService.selectClassificationVo();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<ClassificationVo> json(@RequestBody ClassificationQuery query)
    {
        Page<Classification> page = new Page<Classification>(query.getPage(),query.getRows());
        page = classificationService.selectPage(page);
        Page page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<ClassificationVo> objects = new ArrayList<>();
        for (Classification record : page.getRecords()) {
           ClassificationVo classificationVo = JSON.parseObject(JSON.toJSONString(record), ClassificationVo.class);
            if (StringUtils.isEmpty( record.getUserWholenameId())){
                objects.add(classificationVo);
                continue;
            }
            Wholename wholename = userFeignClient.selectById(record.getUserWholenameId());
            classificationVo.setUser(wholename.getUser());
            classificationVo.setGender(wholename.getGender());
            classificationVo.setLabel(wholename.getLabel());
            classificationVo.setUserWholenameId(wholename.getId());
            objects.add(classificationVo);
        }
        page1.setRecords(objects);
        return new PageList<ClassificationVo>(page1.getTotal(), page1.getRecords());
    }
}
